

import java.io.IOException;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessionbeans.FriendshipBeanRemote;
import sessionbeans.ProfileBeanRemote;
import entities.Declares;
import entities.RegisteredUser;

/**
 * Servlet implementation class ProfileServlet
 */
public class ProfileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfileServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		InitialContext context;
		RegisteredUser visitinguser;
		List<Declares> abilities;
		String userId = (String) request.getSession().getAttribute("userId");
		String user = (String) request.getParameter("user");
		String friendshipState;
		
		try {
			context = new InitialContext();
			Object ref = context.lookup("ProfileBeanRemote");
			ProfileBeanRemote profileBean = (ProfileBeanRemote)PortableRemoteObject.narrow(ref, ProfileBeanRemote.class);
			visitinguser = profileBean.getUser(user);
			abilities = profileBean.getAbilities(user);
			request.getSession().setAttribute("visitinguser", visitinguser);
			request.getSession().setAttribute("visitinguserabilities", abilities);
			
			context = new InitialContext();
			ref = context.lookup("FriendshipBeanRemote");
			FriendshipBeanRemote friendshipBean = (FriendshipBeanRemote)PortableRemoteObject.narrow(ref, FriendshipBeanRemote.class);
			friendshipState = friendshipBean.getAlreadyFriendsOrAsked(userId, user);
			request.getSession().setAttribute("friendshipstate",friendshipState);
			response.sendRedirect("ProfilePage.jsp");
		} catch (NamingException e) {
			System.out.println("Naming Exception");
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
